using Bean.Entity;
using Bean.Entity.Param;
using Bean.Infrastructure;
using Bean.Repository;
using Bean.Service.IService;
using Microsoft.Extensions.Logging;
using SqlSugar;

namespace Bean.Service.Service;

/// <summary>
/// 
/// </summary>
public class PolicyPlanRuleTaskService : IPolicyPlanRuleTaskService, ITransientService
{
    private readonly ILogger<PolicyPlanRuleTaskService> _logger;

    private readonly IRepository<PolicyPlanTask> _policyPlanTaskRepository;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="logger"></param>
    /// <param name="policyPlanTaskRepository"></param>
    public PolicyPlanRuleTaskService(ILogger<PolicyPlanRuleTaskService> logger,
        IRepository<PolicyPlanTask> policyPlanTaskRepository)
    {
        _logger = logger;
        _policyPlanTaskRepository = policyPlanTaskRepository;
    }

    /// <summary>
    /// 根据条件获取政策计划规则任务
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<List<PolicyPlanRuleTaskDto>> GetPolicyPlanTasksAsync(PolicyRuleTaskParam param)
    {
        var response = await _policyPlanTaskRepository.AsQueryable()
            .LeftJoin<PolicyRulePlan>((task, plan) => task.RuleId == plan.SysPolicyRuleId)
            .Where(task => task.Status == param.Status && task.SID == param.SID)
            .Select((task, plan) => new PolicyPlanRuleTaskDto
            {
                RulePolicyTaskId = task.RulePolicyTaskId,
                PolicyKeyWord = task.PolicyKeyWord,
                CreateTime = task.CreateTime,
                Status = task.Status,
                Remarks = task.Remarks,
                RuleText = plan.RuleText,
                TaskStartTime = task.TaskStartTime,
                TaskEndTime = task.TaskEndTime,
                RuleId = task.RuleId,
                OperateType = task.OperateType,
            }).ToListAsync();

        return response;
    }

    /// <summary>
    /// 通过任务ID获取任务计划详情信息
    /// </summary>
    /// <param name="taskId"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<PolicyPlanRuleTaskDto> GetTaskPlanAsync(long taskId)
    {
        var response = await _policyPlanTaskRepository.AsQueryable()
            .LeftJoin<PolicyRulePlan>((task, plan) => task.RuleId == plan.SysPolicyRuleId)
            .Where(task => task.RulePolicyTaskId == taskId).Select((task, plan) => new PolicyPlanRuleTaskDto
            {
                RulePolicyTaskId = task.RulePolicyTaskId,
                PolicyKeyWord = task.PolicyKeyWord,
                CreateTime = task.CreateTime,
                Status = task.Status,
                Remarks = task.Remarks,
                RuleText = plan.RuleText,
                TaskStartTime = task.TaskStartTime,
                TaskEndTime = task.TaskEndTime,
                RuleId = task.RuleId,
                OperateType = task.OperateType,
            }).FirstAsync();

        return response;
    }

    /// <summary>
    /// 修改任务状态
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public Task<bool> UpdateTaskStatusAsync()
    {
        throw new NotImplementedException();
    }
}